
package com.swak.frame.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

/**
 * ClassName: MapBean.java
 * 
 * @Description: map转换
 * @author colley.ma
 * @date 2021年3月15日
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class MapBean {
	private Map result;

	public MapBean(Map result) {
		this.result = result;
	}

	public int size() {
		if (result != null) {
			return result.size();
		}

		return 0;
	}

	public String getString(String key) {
		return getString(key, "");
	}

	public String getString(String key, String defaultValue) {
		return MapUtils.getString(result, key, defaultValue);
	}

	public Map<String, ?> getMap(String key) {
		return  (Map<String, ?>) MapUtils.getMap(result, key);
	}

	public Integer getInteger(String key) {
		return getInteger(key, 0);
	}

	public Integer getInteger(String key, Integer defaultValue) {
		return MapUtils.getInteger(result, key, defaultValue);
	}

	public Long getLong(String key, Long defaultValue) {
		return MapUtils.getLong(result, key, defaultValue);
	}

	public Long getLong(String key) {
		return getLong(key, 0L);
	}

	public Double getDouble(String key) {
		return getDouble(key, 0d);
	}

	public Object getObject(String key) {
		return MapUtils.getObject(result, key);
	}

	public Collection<?> getCollection(String key) {
		Object obj = getObject(key);
		if (obj == null) {
			return null;
		}
		if (obj instanceof Collection<?>) {
			return (Collection<?>) obj;
		}
		return null;
	}

	public List<?> getList(String key) {
		Collection<?> collection = getCollection(key);
		if (collection == null) {
			return null;
		}
		if (collection instanceof List<?>) {
			return (List<?>) collection;
		}
		return new ArrayList<>(collection);
	}

	public Double getDouble(String key, Double defaultValue) {
		return MapUtils.getDouble(result, key, defaultValue);
	}
}
